use strict;
use warnings;

package Lingua::EN::WSD::WebBased::Seeder::DmozLabel;

use base qw/Lingua::EN::WSD::WebBased::Seeder/;

use Lingua::EN::WSD::WebBased::Logger;
use Lingua::EN::WSD::WebBased::Utils;
use Lingua::EN::WSD::WebBased::Vectorizer;

use List::MoreUtils qw/uniq/;

use Class::AccessorMaker {
    dmoz_structure_db   => undef,
    wordnet             => undef
}, 'new_init';

sub init {
    
    my $self = shift;

    $self->SUPER::init;

    check_type 'attribute wordnet'
        => $self->wordnet => 'WordNet::QueryData';
    check_type 'attribute dmoz_structure_db'
        => $self->dmoz_structure_db => 'HASH';

}

# generate seeds for a dmoz label

sub gen_seeds {

    my ($self, $label_with_path) = @_;

    _trace(__PACKAGE__, "::gen_seeds($label_with_path)");

    my $parent_label_with_path = $label_with_path;
    $parent_label_with_path =~ s/\/[^\/]+$//;

    my $label_info = query_dmoz_structure_db
        $self->dmoz_structure_db, $label_with_path;
    my $parent_label_info = query_dmoz_structure_db
        $self->dmoz_structure_db, $parent_label_with_path;
    
    my ($label, @ancestors) = reverse split /\//, $label_with_path;
    _error("invalid label: $label_with_path") if !defined $label || !@ancestors;

    my $v = new Lingua::EN::WSD::WebBased::Vectorizer(wordnet => $self->wordnet);

    # seeds from ancestors
    
    my @seeds = keys %{
        # parent and grand parent labels
        $v->vectorize(join(' ', map { $ancestors[$_] || '' } (0..1))),
        # children labels
        $v->vectorize(join(' ', @{$label_info->{subtopic}})),
        # sibling labels
        $v->vectorize(join(' ', @{$parent_label_info->{subtopic}})),
    };

    @seeds = uniq @seeds;
    @seeds = sort @seeds if $self->sort_seeds;
    
    return \@seeds;

}

sub gen_extra {

    my ($self, $label_with_path) = @_;

    _trace(__PACKAGE__, "::gen_extra($label_with_path)");

    my $label_info = query_dmoz_structure_db
        $self->dmoz_structure_db, $label_with_path;

    return $label_info->{desc} || '';

}

sub gen_query_core {

    my ($self, $label) = @_;
    return do { $label =~ /\/([^\/]+)$/; $1 };

}

1;
